using System;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Data.Core;
using Avalonia.Data.Core.Plugins;
using Avalonia.Markup.Xaml;
using log4net;
using ui_test.ViewModels;
using ui_test.Views;

namespace ui_test;

public partial class App : Application
{
    private static readonly ILog log = LogManager.GetLogger(typeof(App));

    public override void Initialize()
    {
        AvaloniaXamlLoader.Load(this);
        AppDomain.CurrentDomain.UnhandledException += (sender, e) => { log.Fatal(e); };
        TaskScheduler.UnobservedTaskException += ((sender, args) => { log.Fatal(args.Exception); });
    }
     

    public override void OnFrameworkInitializationCompleted()
    {
        if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
        {
            // Line below is needed to remove Avalonia data validation.
            // Without this line you will get duplicate validations from both Avalonia and CT
            BindingPlugins.DataValidators.RemoveAt(0);
            desktop.MainWindow = new MainWindow
            {
                DataContext = new MainWindowViewModel(),
            };
        }

        base.OnFrameworkInitializationCompleted();
    }

    protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
    {
        base.OnPropertyChanged(change);
        Console.WriteLine($"property changed  {change.OldValue} -> {change.NewValue}");
    }

    public override void RegisterServices()
    {
        base.RegisterServices();
        Console.WriteLine("application reg end");
    }
}